package com.cnzz.site1255174697.fragment.main;

import android.app.Dialog;
import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ListView;
import android.widget.RelativeLayout;

import com.cnzz.site1255174697.R;
import com.cnzz.site1255174697.activity.taobao.KindsListActivity;
import com.cnzz.site1255174697.activity.taobao.SearchActivity;
import com.cnzz.site1255174697.factory.IndexFactory;
import com.cnzz.site1255174697.holder.KindsGoodsGridAdapter;
import com.cnzz.site1255174697.holder.KindsListAdapter;
import com.cnzz.site1255174697.holder.KindsTopAdapter;
import com.cnzz.site1255174697.model.BaseEntity;
import com.cnzz.site1255174697.model.KindsTopEntity;
import com.cnzz.site1255174697.utils.AlibcTradeUtils;
import com.cnzz.site1255174697.utils.SystemBarUtils;
import com.cqyanyu.yanyu.http.XCallback;
import com.cqyanyu.yanyu.http.XResultList;
import com.cqyanyu.yanyu.utils.CustomDialogUtil;
import com.cqyanyu.yanyu.utils.XToastUtil;

import org.xutils.common.util.LogUtil;

/**
 * Created by Administrator on 2016/8/29.
 * 分类
 */
public class KindsFragment extends Fragment implements View.OnClickListener {

    protected View rootView;
    protected RelativeLayout btSearch;
    protected GridView tGridView;
    protected ListView nameList;
    protected GridView goodsGrid;
    private KindsTopAdapter kindsTopAdapter;//顶部
    private KindsGoodsGridAdapter kindsGoodsGridAdapter;//商品列表
    private KindsListAdapter kindsListAdapter;//分类名列表

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.fragment_kinds, null);
        initView();
        SystemBarUtils.setSystemBarColor(getActivity(), rootView);
        loadData();
        return rootView;
    }

    //初始化
    private void initView() {
        btSearch = (RelativeLayout) rootView.findViewById(R.id.bt_search);
        tGridView = (GridView) rootView.findViewById(R.id.tGridView);
        nameList = (ListView) rootView.findViewById(R.id.name_list);
        goodsGrid = (GridView) rootView.findViewById(R.id.goods_grid);
        btSearch.setOnClickListener(this);


    }

    //加载数据
    private void loadData() {
        IndexFactory.getIndexTop(getActivity(), "", new XCallback<XResultList<BaseEntity>>() {
            @Override
            public void onSuccess(XResultList<BaseEntity> result) {
                result.data.remove(0);
                kindsListAdapter = new KindsListAdapter(getActivity(), result.data);
                nameList.setAdapter(kindsListAdapter);
                kindsListAdapter.setSelect(0);
                getGoods(kindsListAdapter.getItem(0).getKey_id());
            }

            public void onFinished() {
            }
        });
        IndexFactory.getTopType(getActivity(), new XCallback<XResultList<KindsTopEntity>>() {
            @Override
            public void onSuccess(XResultList<KindsTopEntity> result) {
                kindsTopAdapter = new KindsTopAdapter(getActivity(), result.data);
                tGridView.setAdapter(kindsTopAdapter);
            }

            @Override
            public void onFinished() {
            }
        });
        itemOnClick();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bt_search:
                startActivity(new Intent(getActivity(), SearchActivity.class));
                break;
        }
    }

    private void itemOnClick() {
        nameList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                kindsListAdapter.setSelect(position);
                getGoods(kindsListAdapter.getItem(position).getKey_id());
            }
        });
        tGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                AlibcTradeUtils.showUrl(getActivity(), kindsTopAdapter.getItem(position).getUrl());

            }
        });
        goodsGrid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                startActivity(new Intent(getActivity(), KindsListActivity.class).putExtra("title", kindsGoodsGridAdapter.getItem(position).getName()).
                        putExtra("type", kindsGoodsGridAdapter.getItem(position).getKey_id()).putExtra("name", kindsGoodsGridAdapter.getItem(position).getPname()));
            }
        });
    }

    private void getGoods(String pid) {
        final Dialog dialog = CustomDialogUtil.showLoadDialog(getActivity(), "加载中...");
        IndexFactory.getIndexTop(getActivity(), pid, new XCallback<XResultList<BaseEntity>>() {
            @Override
            public void onSuccess(XResultList<BaseEntity> result) {
                kindsGoodsGridAdapter = new KindsGoodsGridAdapter(getActivity(), result.data);
                goodsGrid.setAdapter(kindsGoodsGridAdapter);
            }

            public void onFinished() {
                dialog.dismiss();
            }
        });
    }
}
